from fastapi import APIRouter, File, UploadFile
from sqlalchemy.orm import Session
from fastapi import Depends
from utils import token
from extend.get_db import init_db
from models.user.user_operation import get_user_pagenation,get_user_total,active,user_info_updata,user_info_del,add_users,get_user_state_total
from  fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel,Field
from typing import Optional
router = APIRouter()
#查询参数
@router.get("/user_list")
def user_list(page_size:int,currentPage:int,id:str = Depends(token.pass_token),db:Session = Depends(init_db)):

    users = get_user_pagenation(db,page_size,currentPage)
    total = get_user_total(db)
    userlist = []
    for i in users:
        dic = {
            'id':i.id,
            'username':i.username,
            'addr':i.addr,
            'create_time': i.create_time,
            'state':i.state,
            'avatar':i.avatar,
            'dep_id':i.dep_id
        }
        userlist.append(dic)
    # print(userlist)

    #字典里的列表如果识别不了的话 使用jsonable_encoder转换
    content = {
        'code':200,
        'msg':'查询成功',
        'userlist':jsonable_encoder(userlist),
        'page_size':page_size,
        'pageToal':total,
        'currentPage':currentPage
    }
    return JSONResponse(content=content)
    # return 11

class user_state(BaseModel):
        id:int
        state:int


#修改用户状态修改接口
@router.post("/active")
def active_user(user:user_state,id:str = Depends(token.pass_token),db:Session = Depends(init_db)):
   print(id)
   if id != "1": return {"code": 500, 'msg': '不是管理员，禁止操作'}

   if user.id == 1: return {"code": 500, 'msg': '禁止添加或编辑管理员用户'}
   msg= ''
   state = 1
   if user.state == 1:
      #正在停用
      state = 0
      msg = '停用成功'

   elif user.state == 0:
       #正在启用
      state = 1
      msg = '启用成功'

   active(db,user.id,state)
   return {'code':200,"msg":msg}



class user_updata(BaseModel):

        id:  Optional[int]
        username: str
        addr: str
        create_time:  Optional[str]
        state: Optional[int]
        dep_id: Optional[int]
        avatar: str


#编辑用户接口
@router.post("/update_user")
def upload(user:user_updata,id:str = Depends(token.pass_token),db:Session = Depends(init_db)):
        print(id)
        if id != "1": return {"code": 500, 'msg': '不是管理员，禁止操作'}
        if user.id == 1: return {"code": 500, 'msg': '权限不足'}
        # print(user.id)
        # if user.username == "admin":return {"code":500,'msg':'禁止添加或编辑admin'}
        user_info_updata(db,user.id,user)
        return {"code":200,'msg':'编辑成功'}






#删除用户接口
@router.post("/del_user")
def del_user(user:user_updata,id:str = Depends(token.pass_token),db:Session = Depends(init_db)):
  if id != "1": return {"code": 500, 'msg': '不是管理员，禁止操作'}
  if user.id == 1: return {"code": 500, 'msg': '禁止添加或编辑管理员用户'}
  user_info_del(db,user.id)
  return JSONResponse(content={"code":200,"msg":"删除成功","id":user.id})


class user_adddata(BaseModel):
    username: str
    addr: str
    state: Optional[int]
    avatar: str


#添加用户
@router.post("/add_user")
def adduser(user:user_adddata,id:str = Depends(token.pass_token),db:Session = Depends(init_db)):
   # print(user)
   if id != "1": return {"code": 500, 'msg': '不是管理员，禁止操作'}
   if user.username == "admin": return {"code": 500, 'msg': '禁止添加或编辑admin'}
   add_users(db,user)
   return {"code":200,"msg":"添加成功"}


#用户总数 和 启停用户各个数量
@router.get("/user_state_list")
def user_state_list(id:str = Depends(token.pass_token),db:Session = Depends(init_db)):
     total = get_user_total(db)
     yn_total = get_user_state_total(db)

     # print(yn_total)
     return  [total,yn_total['y_total'],yn_total['n_total']]